package com.wyg.blog.service.serviceimpl;

import com.wyg.blog.service.blog.MailService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Service;

import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;

/**
 * @description:
 * @author: WYG
 * @time: 2021/2/3 0:04
 */
@Service
public class MailServiceImpl implements MailService {
    @Autowired
    private JavaMailSender javaMailSender;

    @Value("${spring.mail.username}")
    private String MailFrom;

    @Override
    public void sendComment(String to, String content) {
        SimpleMailMessage message = new SimpleMailMessage();
        message.setFrom(MailFrom);
        message.setTo(to);
        message.setSubject("风在哪个人博客");
        StringBuffer text = new StringBuffer();
        text.append("您的留言收到了回复,请前往查看!");
        text.append(content);
        message.setText(text.toString());
        javaMailSender.send(message);
    }

    @Override
    public void sendMessage(String to, String content) throws MessagingException {
        MimeMessage message = javaMailSender.createMimeMessage();
        MimeMessageHelper helper = new MimeMessageHelper(message,true);
        helper.setFrom(MailFrom);
        helper.setTo(to);
        helper.setSubject("风在哪个人博客");
        helper.setText(content,true);
        javaMailSender.send(message);
    }
}
